package com.ssmr.demo.service;

import com.ssmr.demo.entity.TPurchaseRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RabbitmqProducerServiceImpl implements RabbitmqProducerService, RabbitTemplate.ConfirmCallback {

    private static final String SSMR_EXCHANGE = "ssmr_exchange";
    private static final String SSMR_QUEUE = "ssmr_queue";
    private static final String SSMR_ROUTING_KEY = "ssmr_routing_key";
    private static Logger logger = LoggerFactory.getLogger(RabbitmqProducerServiceImpl.class);

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Override
    public void sendPurchaseRecord(TPurchaseRecord tPurchaseRecord) {
        amqpTemplate.convertAndSend(SSMR_EXCHANGE, SSMR_ROUTING_KEY, tPurchaseRecord);

    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            logger.info("rabbitmq消息发送成功");
            /*消息发送，消费成功*/
        } else {
            Date date = new Date();
            logger.info("rabbitmq消息发送失败: " + this.getClass().getSimpleName() + " " + date.toString());
        }
    }
}
